package com.keeson.android_cli.manager;

import android.app.Activity;


import com.keeson.android_cli.ui.main.MainActivity;
import com.keeson.android_cli.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiaqiZHou
 * on 2020/12/4
 */
public class ActivityManager {

    private static List<Activity> mActivityStack = new ArrayList<>();

    private ActivityManager() {

    }

    public static ActivityManager getInstance() {
        return ActivityManagerHolder.mInstance;
    }

    public static class ActivityManagerHolder {

        public static ActivityManager mInstance = new ActivityManager();

    }

    public void addActivity(Activity activity) {

        mActivityStack.add(activity);

    }

    public void removeActivity(Activity activity) {

        mActivityStack.remove(activity);

    }

    public void finishToHome() {

        if (CollectionUtils.isEmpty(mActivityStack)) {
            return;
        }
        for (Activity activity : mActivityStack) {
            if (activity != null && !activity.isFinishing() && !(activity instanceof MainActivity)) {
                activity.finish();
            }
        }
        mActivityStack.clear();

    }




    public void finishAllActivity() {

        if (CollectionUtils.isEmpty(mActivityStack)) {
            return;
        }
        for (Activity activity : mActivityStack) {
            if (activity != null && !activity.isFinishing()) {
                activity.finish();
            }
        }
        mActivityStack.clear();

    }

}
